package com.cskaoyan.service.allusers;

import com.cskaoyan.mapper.*;
import com.cskaoyan.model.bean.*;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.vo.allusersvo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AllUsersServiceImpl implements AllUsersService {

    @Autowired
    UserMapper userMapper;


    @Override
    public UsersListVo getUserList(PageBo pageBo, String username, String mobile) {

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        UserExample userExample = new UserExample();
        userExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());//根据￥做sql语句拼接
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdIsNotNull()
                .andDeletedEqualTo(false)

        ;
        if (mobile != null) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }

        List<User> userList = userMapper.selectByExample(userExample);

        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        long total = userPageInfo.getTotal();

        UsersListVo usersListVo = new UsersListVo();
        usersListVo.setItems(userList);
        usersListVo.setTotal(total);
        return usersListVo;
    }

    @Autowired
    AddressMapper addressMapper;

    @Override
    public AddressListVo getAddressList(PageBo pageBo, String name, Integer id) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        AddressExample addressExample = new AddressExample();
        addressExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andIdIsNotNull().andDeletedEqualTo(false);

        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }

        List<Address> addresses = addressMapper.selectByExample(addressExample);

        PageInfo<Address> addressPageInfo = new PageInfo<>(addresses);
        long total = addressPageInfo.getTotal();
        AddressListVo addressListVo = new AddressListVo();
        addressListVo.setItems(addresses);
        addressListVo.setTotal(total);

        return addressListVo;
    }


    @Autowired
    CollectMapper collectMapper;

    @Override
    public CollectListVo getCollectList(PageBo pageBo, Integer userId, Integer valueId) {

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        CollectExample collectExample = new CollectExample();
        collectExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andIdIsNotNull()
                .andDeletedEqualTo(false);

        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        PageInfo<Collect> collectsPageInfo = new PageInfo<>(collects);
        long total = collectsPageInfo.getTotal();
        CollectListVo collectListVo = new CollectListVo();
        collectListVo.setItems(collects);
        collectListVo.setTotal(total);

        return collectListVo;
    }

    @Autowired
    FootprintMapper footprintMapper;

    @Override
    public FoorprintListVo getFoorprintList(PageBo pageBo, Integer userId, Integer goodsId) {

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        FootprintExample footprintExample = new FootprintExample();
        footprintExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andIdIsNotNull()
                .andDeletedEqualTo(false);

        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }

        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        long total = footprintPageInfo.getTotal();
        FoorprintListVo foorprintListVo = new FoorprintListVo();
        foorprintListVo.setItems(footprints);
        foorprintListVo.setTotal(total);

        return foorprintListVo;
    }

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public HistoryListVo getHistoryList(PageBo pageBo, Integer userId, String keyword) {

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        searchHistoryExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andIdIsNotNull()
                .andDeletedEqualTo(false);

        if (keyword != null) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }

        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        long total = searchHistoryPageInfo.getTotal();
        HistoryListVo historyListVo = new HistoryListVo();
        historyListVo.setItems(searchHistories);
        historyListVo.setTotal(total);
        return historyListVo;
    }

    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public FeedbackLisVo getFeedbackList(PageBo pageBo, Integer userId, String username) {

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        FeedbackExample feedbackExample = new FeedbackExample();
        feedbackExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        criteria.andIdIsNotNull()
                .andDeletedEqualTo(false)
                .andUsernameLike("%" + username + "%");

        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }

        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        PageInfo<Feedback> feedbackPageInfo = new PageInfo<>(feedbacks);
        long total = feedbackPageInfo.getTotal();
        FeedbackLisVo feedbackLisVo = new FeedbackLisVo();
        feedbackLisVo.setItems(feedbacks);
        feedbackLisVo.setTotal(total);


        return feedbackLisVo;
    }
}
